In [382]:
import networkx as nx
import nxviz as nz
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

In [383]:
import warnings
warnings.filterwarnings('ignore')

In [384]:
sns.set()

In [385]:
# import karate club
G = nx.karate_club_graph()

In [386]:
clubs = [n[1]['club'] for n in G.nodes(data=True)]
unique_clubs = np.unique(clubs)
print(unique_clubs)


['Mr. Hi' 'Officer']

In [387]:
# membership breakdown
_ = sns.countplot(clubs)
plt.show()



In [388]:
# degree distribution
degrees = [len(list(G.neighbors(n))) for n in G.nodes()]

bins = int(np.sqrt(len(degrees)))
_ = plt.hist(degrees, bins=bins, normed=True)
plt.show()



In [389]:
# show club affiliation
_ = nz.ArcPlot(G, node_color='club', node_order='club')
_.draw()

plt.show()



In [390]:
degree_centrality = nx.degree_centrality(G)
betweenness_centrality = nx.betweenness_centrality(G)
closeness_centrality = nx.closeness_centrality(G)

In [391]:
def set_club_colors(G):
    for node in G.nodes(data=True):
        # Mr. Hi = 'purple', Officier = 'blue'
        color = '#00fff9'
        if node[1]['club'] == 'Mr. Hi':
            color = '#e6e6fa'

        node[1]['color'] = color

In [392]:
# set centrality scores, affiliation color
for node in G.nodes(data=True):
    _id = node[0]
    
    node[1]['dc'] = degree_centrality[_id]
    node[1]['bc'] = betweenness_centrality[_id]
    node[1]['cc'] = closeness_centrality[_id]

set_club_colors(G)

In [393]:
def pull_first_occurrence_from_club(lookup, club, not_node=-1):
    for item in lookup:
        if item[0] == not_node:
            continue
        
        if item[1]['club'] == club:
            return item
        
    return None

In [394]:
# split out and sort by scores
sorted_nodes_by_degree_centrality = sorted(G.nodes(data=True), key=lambda node: node[1]['dc'], reverse=True)
sorted_nodes_by_betweenness_centrality = sorted(G.nodes(data=True), key=lambda node: node[1]['bc'], reverse=True)
sorted_nodes_by_closeness_centrality = sorted(G.nodes(data=True), key=lambda node: node[1]['cc'], reverse=True)

In [395]:
# set club's most popular
n1 = pull_first_occurrence_from_club(sorted_nodes_by_degree_centrality, 'Mr. Hi')
n2 = pull_first_occurrence_from_club(sorted_nodes_by_degree_centrality, 'Officer')

n1[1]['color'] = '#800080'
n2[1]['color'] = '#0000ff'

mr_hi_president = n1[0]
officer_president = n2[0]

In [396]:
layout_position = nx.spring_layout(G)

In [397]:
colors = [n[1]['color'] for n in G.nodes(data=True)]
nx.draw_networkx(G, pos=layout_position, node_color=colors)

plt.axis('off')
plt.show()

print('Most Popular "' + n1[1]['club'] + '" Member:', n1[0], '(degree centrality=' + str(n1[1]['dc']) + ')')
print('Most Popular "' + n2[1]['club'] + '" Member:', n2[0], '(degree centrality=' + str(n2[1]['dc']) + ')')


Most Popular "Mr. Hi" Member: 0 (degree centrality=0.48484848484848486)
Most Popular "Officer" Member: 33 (degree centrality=0.5151515151515151)

In [398]:
# the club's best bridges (betweeness)
n1 = pull_first_occurrence_from_club(sorted_nodes_by_betweenness_centrality, 'Mr. Hi', mr_hi_president)
n2 = pull_first_occurrence_from_club(sorted_nodes_by_betweenness_centrality, 'Officer', officer_president)

n1[1]['color'] = '#520080'
n2[1]['color'] = '#0092ff'

In [399]:
colors = [n[1]['color'] for n in G.nodes(data=True)]
nx.draw_networkx(G, pos=layout_position, node_color=colors)

plt.axis('off')
plt.show()

print('Best Bridge (not prez) "' + n1[1]['club'] + '" Member:', n1[0], '(betweeness centrality=' + str(n1[1]['bc']) + ')')
print('Best Bridge (not prez) "' + n2[1]['club'] + '" Member:', n2[0], '(betweeness centrality=' + str(n2[1]['bc']) + ')')


Best Bridge (not prez) "Mr. Hi" Member: 2 (betweeness centrality=0.14365680615680618)
Best Bridge (not prez) "Officer" Member: 32 (betweeness centrality=0.145247113997114)

In [400]:
# imagine if 2, 32 switched ... #8 would def. follow right? or 32 could go the other way ...
nodes = [ 2 ] + list(G.neighbors(2)) + [ 32 ] + list(G.neighbors(32))
sg = nx.subgraph(G, nodes)

sg.node[2]['color'] = '#ff69b4'
colors = [n[1]['color'] for n in sg.nodes(data=True)]

sg_layout_position = nx.spring_layout(sg)
nx.draw_networkx(sg, pos=sg_layout_position, node_color=colors)

plt.axis('off')
plt.show()



In [401]:
# say 2 and 32 moved to a different state ...
nodes = list(G.neighbors(2)) + list(G.neighbors(32))
nodes.remove(2)
nodes.remove(32)

sg = nx.subgraph(G, nodes)

colors = [n[1]['color'] for n in sg.nodes(data=True)]

sg_layout_position = nx.spring_layout(sg)
nx.draw_networkx(sg, pos=sg_layout_position, node_color=colors)

plt.axis('off')
plt.show()



In [402]:
# the club's members closest to all others in the entire network (closeness)
n1 = pull_first_occurrence_from_club(sorted_nodes_by_closeness_centrality, 'Mr. Hi', mr_hi_president)
n2 = pull_first_occurrence_from_club(sorted_nodes_by_closeness_centrality, 'Officer', officer_president)

n1[1]['color'] = '#2b0080'
n2[1]['color'] = '#ff69b4'

In [403]:
colors = [n[1]['color'] for n in G.nodes(data=True)]
nx.draw_networkx(G, pos=layout_position, node_color=colors)

plt.axis('off')
plt.show()

print('Most close (not prez) "' + n1[1]['club'] + '" Member:', n1[0], '(closeness centrality=' + str(n1[1]['bc']) + ')')
print('Most close (not prez) "' + n2[1]['club'] + '" Member:', n2[0], '(closeness centrality=' + str(n2[1]['bc']) + ')')


Most close (not prez) "Mr. Hi" Member: 2 (closeness centrality=0.14365680615680618)
Most close (not prez) "Officer" Member: 31 (closeness centrality=0.13827561327561325)

In [404]:
# reset club colors
set_club_colors(G)

Club level


In [405]:
# look at Mr. Hi's club
nodes = [n[0] for n in G.nodes(data=True) if n[1]['club'] == 'Mr. Hi']
mr_hi_club = nx.subgraph(G, nodes)

colors = [n[1]['color'] for n in mr_hi_club.nodes(data=True)]
mr_hi_layout_position = nx.spring_layout(mr_hi_club)
nx.draw_networkx(mr_hi_club, pos=mr_hi_layout_position, node_color=colors)

plt.axis('off')
plt.show()



In [406]:
# degree distribution of mr hi
degrees = [len(list(mr_hi_club.neighbors(n))) for n in mr_hi_club.nodes()]
bins = int(np.sqrt(len(degrees)))

_ = plt.subplot(1,2,1)
_ = plt.hist(degrees, bins=bins)

_ = plt.subplot(1,2,2)
_ = plt.hist(degrees, bins=bins, normed=True)

plt.tight_layout()
plt.show()



In [407]:
# look at officer's club
nodes = [n[0] for n in G.nodes(data=True) if n[1]['club'] == 'Officer']
officer_club = nx.subgraph(G, nodes)

colors = [n[1]['color'] for n in officer_club.nodes(data=True)]
officer_club_layout_position = nx.spring_layout(officer_club)
nx.draw_networkx(officer_club, pos=officer_club_layout_position, node_color=colors)

plt.axis('off')
plt.show()



In [408]:
# degree distribution of officer club
degrees = [len(list(officer_club.neighbors(n))) for n in officer_club.nodes()]
bins = int(np.sqrt(len(degrees)))

_ = plt.subplot(1,2,1)
_ = plt.hist(degrees, bins=bins)

_ = plt.subplot(1,2,2)
_ = plt.hist(degrees, bins=bins, normed=True)

plt.tight_layout()
plt.show()



In [ ]: